package com.gcsoftware.web.service.elasticsearch;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gcsoftware.web.beans.T_GW_DSITEM;
import com.gcsoftware.web.beans.elasticsearchData.Agg;
import com.gcsoftware.web.beans.elasticsearchData.Hit;
import com.gcsoftware.web.beans.returnPage.ApiCount;
import com.gcsoftware.web.beans.returnPage.AsensitiveItem;
import com.gcsoftware.web.beans.returnPage.Buckets;
import com.gcsoftware.web.beans.returnPage.PageObject;
import com.gcsoftware.web.mapper.T_GW_DSITEMMAPPER;
import com.gcsoftware.web.mapper.elasticsearch.ReportDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.gcsoftware.configs.AppConstants.Elasticsearch.*;

/**
 * 报表
 * @author czp
 * data : 2018-04-11
 */
@Service
public class ReportService {
    @Autowired
    private ReportDao reportDao;

    @Autowired
    private T_GW_DSITEMMAPPER t_gw_dsitemmapper;

    public PageObject<ApiCount> apiCountReport(int rows, int page, String appName, String apiName,String sord, String range) {
        Page<Hit<ApiCount>> esPage = reportDao.apiCountReport(new PageRequest(page - 1, rows), appName, apiName, range, sord);
        Agg<Buckets> esGroup = (Agg<Buckets>) reportDao.apiCountNumber(range);
        ObjectMapper mapper = new ObjectMapper();
        List<ApiCount> rList = new ArrayList<ApiCount>();
        for (int i = 0; i < esPage.getContent().size(); i++) {
            ApiCount pojo = mapper.convertValue(esPage.getContent().get(i).getSource(), ApiCount.class);
            pojo.setId(esPage.getContent().get(i).getId());
            for (int j = 0; j < esGroup.getBuckets().size(); j++) {
                Buckets buckets = mapper.convertValue(esGroup.getBuckets().get(j), Buckets.class);
                if (buckets.getKey().trim().equals(pojo.getApiName().trim())) {
                    pojo.setCount(buckets.getDoc_count());
                    break;
                }
            }
            rList.add(pojo);
        }
        return new PageObject<ApiCount>(
            page,
            (int) esPage.getTotalElements() / esPage.getSize() + 1,
            (int) esPage.getTotalElements(),
            rList
        );
    }

    public List<AsensitiveItem> asensitiveItemCountReport(String asensitiveItem, String range) {
        List<AsensitiveItem> rList = new ArrayList<>();
        List<T_GW_DSITEM> asensitiveItems;
        if (asensitiveItem != null && !"".equals(asensitiveItem.trim())) {
            asensitiveItems = t_gw_dsitemmapper.queryForList(asensitiveItem);
            rList.add(new AsensitiveItem(
                1,
                asensitiveItems.get(0).getC_DS_ITEM_ID().trim(),
                asensitiveItems.get(0).getC_DS_ITEM_NAME().trim(),
                reportDao.asensitiveItemCount(asensitiveItems.get(0).getC_DS_ITEM_NAME().trim(),
                        ES_SENSITIVE_ITEM_NAMES.getValue(), ES_MATCH_PHRASE_TAG.getValue(), range)
            ));
        } else {
            asensitiveItems = t_gw_dsitemmapper.queryForList(null);
            for (int i = 0; i < asensitiveItems.size(); i++) {
                rList.add(new AsensitiveItem(
                    i + 1,
                    asensitiveItems.get(i).getC_DS_ITEM_ID().trim(),
                    asensitiveItems.get(i).getC_DS_ITEM_NAME().trim(),
                    reportDao.asensitiveItemCount(asensitiveItems.get(i).getC_DS_ITEM_NAME().trim(),
                            ES_SENSITIVE_ITEM_NAMES.getValue(), ES_MATCH_PHRASE_TAG.getValue(), range)
                ));
            }
        }
        return rList;
    }

    public Set<String> getApiNames() {
        Agg<Buckets> es = (Agg<Buckets>) reportDao.getApiNames();
        return Mapper(es);
    }

    public Set<String> getAppNames() {
        Agg<Buckets> es = (Agg<Buckets>) reportDao.getAppNames();
        return Mapper(es);
    }

    private Set<String> Mapper(Agg<Buckets> es) {
        ObjectMapper mapper = new ObjectMapper();
        Set<String> rSet = new HashSet<String>();
        if (es == null) return null;
        for (int i = 0; i < es.getBuckets().size(); i++) {
            Buckets buckets = mapper.convertValue(es.getBuckets().get(i), Buckets.class);
            String key = buckets.getKey().trim();
            if (StringUtils.isNotBlank(key)) {
                List<String> keys = com.gcsoftware.utils.elasticsearch.StringUtils.StringSplit(key, ES_STR_SPLIT.getValue());
                for (String k : keys) {
                    rSet.add(k);
                }
            }
        }
        return rSet;
    }
}
